<template>
  <view>
    <view class="goods-list" >
      <view v-for="goods,i in goodsList" :key='i' @click="gotoDetail(goods)">
        <my-goods :goods='goods'></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求参数对象
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        //商品列表
        goodsList: [],
        total:0,
        //节流阀
        isloading:false
      };
    },
    onLoad(options) {
      // 将页面参数转存到 this.queryObj 对象中
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods: {
      //点击列表去到对应的详情页
      gotoDetail(goods){
        uni.navigateTo({
          url:'/subpkg/goods_datail/goods_datail?goods_id='+goods.goods_id
        })
      },
      async getGoodsList(callback) {
        this.isloading=true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        this.isloading=false
        //回调函数
        callback && callback()
        if(res.meta.status!==200) return uni.$showMsg()
        this.goodsList=[...this.goodsList,...res.message.goods]
        this.total=res.message.total
      }
    },
    //上拉加载更多
    onReachBottom() {
      if(this.queryObj.pagenum*this.queryObj.pagesize >= this.total) return uni.$showMsg()
      if(this.isloading) return
      //让页码自加 1
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    //下拉刷新
    onPullDownRefresh() {
      //重置关键数据
      this.queryObj.pagenum=1
      this.total=0
      this.isloading=false
      this.goodsList=[]
      //重新发起请求 回调停止下拉刷新
      this.getGoodsList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss" scoped>

</style>
